﻿#region Copyright
// Copyright (c) 2011-2012 RaisingForce Team (Alikin Sergey)
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion

using System.Collections.Generic;
using System.Diagnostics.Contracts;
using RaisingForce.Raise.Language.Rules;

namespace RaisingForce.Raise.Language.RStates
{
    sealed public class CurrentChain :
        Internal.ChainContainerState
    {
        #region Constructors

        private CurrentChain(string name)
            : base(name)
        {
            Contract.Requires(ChainRules.NameIsValid(name));
        }

        #endregion

        #region Private Static Declarations

        private static readonly
            Internal.ChainContainerStateCollection<CurrentChain> chainStates =
            new Internal.ChainContainerStateCollection<CurrentChain>();

        static CurrentChain()
        {
            foreach (string allowedChainName in SharedChainData.AllowedChainNames)
            {
                Contract.Assume(ChainRules.NameIsValid(allowedChainName));
                CurrentChain chainState = new CurrentChain(allowedChainName);
                CurrentChain.chainStates.Add(chainState);
            }
        }

        #endregion

        #region Public Methods

        [Pure]
        public PreviousChain ToPreviousChain()
        {
            Contract.Ensures(Contract.Result<PreviousChain>() != null);
            return PreviousChain.Get(this.Name);
        }

        #endregion

        #region Base Overrides

        protected override bool isDefault
        {
            get
            {
                return object.ReferenceEquals(this, CurrentChain.Default);
            }
        }

        #endregion

        #region Public Static Declarations

        public static CurrentChain Default
        {
            get
            {
                Contract.Ensures(Contract.Result<CurrentChain>() != null);
                return CurrentChain.Get(SharedChainData.DefaultChainName);
            }
        }

        [Pure]
        public static CurrentChain Get(string name)
        {
            Contract.Ensures(Contract.Result<CurrentChain>() != null);
            return CurrentChain.chainStates[name];
        }

        [Pure]
        public static IEnumerator<CurrentChain> GetEnumerator()
        {
            Contract.Ensures(Contract.Result<IEnumerator<CurrentChain>>() != null);
            return CurrentChain.chainStates.GetEnumerator();
        }

        [Pure]
        public static IEnumerable<CurrentChain> GetEnumerable()
        {
            Contract.Ensures(Contract.Result<IEnumerable<CurrentChain>>() != null);
            return CurrentChain.chainStates.GetEnumerable();
        }

        [Pure]
        public static CurrentChain FromPreviousChain(PreviousChain previousChain)
        {
            Contract.Requires(previousChain != null);
            Contract.Requires(!previousChain.IsNone);
            Contract.Ensures(Contract.Result<CurrentChain>() != null);
            return CurrentChain.Get(previousChain.Name);
        }

        #endregion
    }
}
